package com.cysion.study.adapter

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.cysion.other._setOnClickListener
import com.cysion.study.R
import com.cysion.study.entity.AddChooseTiEntity
import com.cysion.study.ktbox.base.BaseAdapter
import com.cysion.study.ktbox.base.BaseViewHolder
import kotlinx.android.synthetic.main.item_add_choose_ti_list.view.*
import com.cysion.study.ui.ModifyChooseTiActivity
import com.cysion.study.widget.ShowToast
import com.cysion.targetfun._addTextChangedListener


class AddChooseTiListAdapter(mEntities: MutableList<AddChooseTiEntity>, mContext: Context) :
    BaseAdapter<AddChooseTiEntity>(mEntities, mContext) {

    companion object {

        //点击事件
        const val preview = 2000
        const val refuse = 2001
        const val agree = 2003
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BaseViewHolder<AddChooseTiEntity> {
        return ProMoneyHolder(
            LayoutInflater.from(mContext).inflate(R.layout.item_add_choose_ti_list, parent, false)
        )
    }

    private var isMore = false

    fun setIsMore(it: Boolean) {
        isMore = it
    }

    class ProMoneyHolder(itemView: View) : BaseViewHolder<AddChooseTiEntity>(itemView) {
        override fun fillData(obj: AddChooseTiEntity, position: Int) {
            itemView?.apply {

                if (!TextUtils.isEmpty(obj.content)) {
                    itemView.et_content.setText(obj.content)
                }
                cb_item.isChecked = obj.check


                et_content.tag = position//存tag值
                et_content._addTextChangedListener {
                    _afterTextChanged {
                        it?.apply {
                            if (length >= 2000) {
                                ShowToast("超过最大字数了")
                            }
                            obj.check=cb_item.isChecked
                            obj.content = et_content.text.toString().trim()
                            (mContext as ModifyChooseTiActivity).saveEditData(position, et_content.text.toString().trim(),cb_item.isChecked)
                        }
                    }
                }

                cb_item._setOnClickListener {
                    println("###条目内状态"+cb_item.isChecked  )
                    obj.check=cb_item.isChecked
                    (mContext as ModifyChooseTiActivity).saveEditData(position, et_content.text.toString().trim(),obj.check)
//                    mOnTypeClickListener?.invoke(obj, position, agree)
                }

                itemView.iv_delete._setOnClickListener {
                    mOnTypeClickListener?.invoke(obj, position, preview)
                }

            }
        }

    }
}